local shengtong = fk.CreateSkill {
  name = "lingling__shengtong",
}

Fk:loadTranslationTable{
  ["lingling__shengtong"] = "圣统",
  [":lingling__shengtong"] = "出牌阶段，你可以拼点，若你没赢，本回合手牌上限-X（X为红色拼点牌数）。然后你执行一项效果以使你手牌数等于"..
  "手牌上限：1.摸两张牌；2.将一张牌当无次数限制的【杀】使用；3.调整手牌至手牌上限，结束回合。",

  ["#lingling__shengtong"] = "圣统：你可以拼点，然后执行一项效果以使你的手牌数等于手牌上限",
  ["#lingling__shengtong-choice"] = "圣统：请选择执行一项以令手牌数等于手牌上限（当前手牌上限为%arg）",
  ["lingling__shengtong2"] = "将一张牌当【杀】使用",
  ["lingling__shengtong3"] = "调整手牌至手牌上限，结束回合",
  ["#lingling__shengtong-hand"] = "圣统：请将一张手牌当【杀】使用",
  ["#lingling__shengtong-equip"] = "圣统：请将装备区一张牌当【杀】使用",
}

shengtong:addEffect("active", {
  anim_type = "control",
  prompt = "#lingling__shengtong",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return not player:isKongcheng()
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and player:canPindian(to_select)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local pindian = player:pindian({target}, shengtong.name)
    if player.dead then return end
    if pindian.results[target].winner ~= player then
      local n = 0
      if pindian.fromCard and pindian.fromCard.color == Card.Red then
        n = n + 1
      end
      if pindian.results[target].toCard and pindian.results[target].toCard.color == Card.Red then
        n = n + 1
      end
      if n > 0 then
        room:addPlayerMark(player, MarkEnum.MinusMaxCards.."-turn", n)
      end
    end
    local choices = {"lingling__shengtong3"}
    local n = player:getHandcardNum() - player:getMaxCards()
    if n == -2 then
      table.insert(choices, "draw2")
    elseif n == 1 then
      for _, id in ipairs(player:getCardIds("h")) do
        local card = Fk:cloneCard("slash")
        card:addSubcard(id)
        card.skillName = shengtong.name
        if #card:getAvailableTargets(player, { bypass_times = true }) > 0 then
          table.insert(choices, "lingling__shengtong2")
          break
        end
      end
    elseif n == 0 and #player:getCardIds("e") > 0 then
      for _, id in ipairs(player:getCardIds("e")) do
        local card = Fk:cloneCard("slash")
        card:addSubcard(id)
        card.skillName = shengtong.name
        if #card:getAvailableTargets(player, { bypass_times = true }) > 0 then
          table.insert(choices, "lingling__shengtong2")
          break
        end
      end
    end
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = shengtong.name,
      prompt = "#lingling__shengtong-choice:::"..player:getMaxCards(),
      all_choices = {"draw2", "lingling__shengtong2", "lingling__shengtong3"},
    })
    if choice == "draw2" then
      player:drawCards(2, shengtong.name)
    elseif choice == "lingling__shengtong2" then
      local prompt = "#lingling__shengtong-hand"
      if n == 0 then
        prompt = "#lingling__shengtong-equip"
      end
      room:askToUseVirtualCard(player, {
        name = "slash",
        skill_name = shengtong.name,
        prompt = prompt,
        cancelable = false,
        card_filter = {
          n = 1,
          cards = n == 0 and player:getCardIds("e") or player:getCardIds("h"),
        },
      })
    elseif choice == "lingling__shengtong3" then
      if n < 0 then
        player:drawCards(-n, shengtong.name)
      elseif n > 0 then
        room:askToDiscard(player, {
          min_num = n,
          max_num = n,
          include_equip = false,
          skill_name = shengtong.name,
          cancelable = false,
        })
      end
      room:endTurn()
    end
  end,
})

return shengtong
